package com.whfc.uni.xxl.job;

import com.alibaba.fastjson.JSONObject;
import com.whfc.XxlJobConfig;
import com.whfc.common.util.DateUtil;
import com.whfc.uni.dao.CarInoutRecordMapper;
import com.whfc.uni.entity.CarInoutRecord;
import com.whfc.uni.enums.Direction;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @Description 生成车辆数据
 * @Author hw
 * @Date 2021-09-10 10:07
 * @Version 1.0
 */
@Component
@ConditionalOnBean(XxlJobConfig.class)
public class CarInoutDemoJob {

    @Autowired
    private CarInoutRecordMapper carInoutRecordMapper;

    /**
     * 进入车牌号存储
     */
    private final Map<Integer, ConcurrentLinkedQueue<String>> carPlateQueue = new HashMap<>();

    @XxlJob("carInoutDemo")
    public void carInoutDemo() {
        String paramStr = XxlJobHelper.getJobParam();
        if (StringUtils.isEmpty(paramStr)) {
            XxlJobHelper.handleSuccess("没有配置对应的请求参数");
            return;
        }
        XxlJobHelper.log("请求参数为：{}", paramStr);
        JSONObject param = JSONObject.parseObject(paramStr);

        List<Integer> deptIds = param.getJSONArray("deptIds").toJavaList(Integer.class);

        List<Date> times = new ArrayList<>();
        if (param.containsKey("startTime") && param.containsKey("endTime")) {
            Date startTime = param.getDate("startTime");
            Date startDate = DateUtil.getDateBegin(startTime);
            Date endTime = param.getDate("endTime");
            Date endDate = DateUtil.getDateEnd(endTime);
            times = DateUtil.getDayListOfMinute(startDate, endDate, 60);
        } else {
            times.add(new Date());
        }
        for (Date date : times) {
            for (Integer deptId : deptIds) {
                ConcurrentLinkedQueue<String> plateQueue;
                if (carPlateQueue.containsKey(deptId)) {
                    plateQueue = carPlateQueue.get(deptId);
                } else {
                    plateQueue = new ConcurrentLinkedQueue<>();
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                //只生成 6点至22点的数据
                if (hour < 6 || hour > 22) {
                    XxlJobHelper.log("没有到生成的时间");
                    continue;
                }
                //三分之一的几率生成数据
                Random random = new Random();
                int i = random.nextInt(3);
                if (i != 0) {
                    XxlJobHelper.log("几率不对！这次就不生成了");
                    continue;
                }
                //编造进入记录
                CarInoutRecord carInRecord = new CarInoutRecord();
                carInRecord.setDeptId(deptId);
                carInRecord.setDeviceName("西一门入口");
                carInRecord.setName("临时车辆");
                int j = random.nextInt(2);
                if(j==0){
                    carInRecord.setName("会员车辆");
                }
                String carPlate = generateCarPlate();
                carInRecord.setLicensePlate(carPlate);
                carInRecord.setDirection(Direction.IN.getValue());
                carInRecord.setImgUrl(carInoutRecordMapper.getRandomImgUrl());
                carInRecord.setTime(DateUtil.addSeconds(calendar.getTime(), random.nextInt(3600)));
                carInoutRecordMapper.insertSelective(carInRecord);
                //保存进入的车辆车牌号
                plateQueue.add(carPlate);
                if (hour >= 15 && !carPlateQueue.isEmpty()) {
                    //15 点以后车辆才出来
                    String plate = plateQueue.poll();
                    CarInoutRecord inoutRecordDTO = new CarInoutRecord();
                    inoutRecordDTO.setDeptId(deptId);
                    inoutRecordDTO.setDeviceName("西一门出口");
                    inoutRecordDTO.setName("临时车辆");
                    inoutRecordDTO.setLicensePlate(plate);
                    inoutRecordDTO.setDirection(Direction.OUT.getValue());
                    inoutRecordDTO.setImgUrl(carInoutRecordMapper.getRandomImgUrl());
                    inoutRecordDTO.setTime(DateUtil.addSeconds(calendar.getTime(), random.nextInt(10800)));
                    carInoutRecordMapper.insertSelective(inoutRecordDTO);
                }
                carPlateQueue.put(deptId, plateQueue);
            }
        }
    }

    /**
     * 生成随机车牌号
     *
     * @return 车牌号
     */
    private String generateCarPlate() {
        String alphas = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        Random random = new Random();
        StringBuilder plate = new StringBuilder();
        plate.append("鄂A");
        // 5位数字/字母
        for (int i = 0; i < 5; i++) {
            plate.append(alphas.charAt(random.nextInt(36)));
        }
        return plate.toString();
    }
}
